﻿using Himp.Platform;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.Erp
{
    public class OutboundRecService : ErpAppService, IOutboundRecService
    {
        private IRepository<OutboundRec, Guid> _OutboundRecRepository;
        private IBizSeqService _bizSeqService;
        private readonly IRepository<OutboundDetl, Guid> _OutboundDetlRepository;
        private readonly IRepository<MatlInv, Guid> _matlInvRepository;
        private InvManager _invManager;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectConfigRepository"></param>
        public OutboundRecService(IRepository<OutboundRec, Guid> OutboundRecRepository
            , IRepository<OutboundDetl, Guid> OutboundDetlRepository
            , IBizSeqService bizSeqService
            , InvManager invManager
            , IRepository<MatlInv, Guid> matlInvRepository)
        {
            _OutboundDetlRepository = OutboundDetlRepository;
            _OutboundRecRepository = OutboundRecRepository;
            _bizSeqService = bizSeqService;
            _invManager = invManager;
            _matlInvRepository = matlInvRepository;
        }

        /// <summary>
        /// 获取入库单列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<OutboundRecDto>> GetOutboundRecListAsync(OutboundRecInput input)
        {
            var iQuery = await _OutboundRecRepository.WithDetailsAsync();
            iQuery = iQuery.Where(i => i.MatlWhseId == input.matlWhseId);
            iQuery = iQuery.WhereIf(!string.IsNullOrWhiteSpace(input.Filter), i => i.OrdNo.Contains(input.Filter));
            iQuery = iQuery.WhereIf(input.Begndata != null, i => i.OutboundTime.Date >= input.Begndata.Value.Date);
            iQuery = iQuery.WhereIf(input.Enddata != null, i => i.OutboundTime.Date <= input.Enddata.Value.Date);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<OutboundRec>, List<OutboundRecDto>>(iQuery.ToList());

                return new PagedResultDto<OutboundRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OutboundRec>, List<OutboundRecDto>>(items.ToList());

                return new PagedResultDto<OutboundRecDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 获取入库单明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<OutboundRecDto> GetOutboundRecAsync(Guid id)
        {
            var OutboundRec = await _OutboundRecRepository.GetAsync(id);

            return ObjectMapper.Map<OutboundRec, OutboundRecDto>(OutboundRec);
        }

        /// <summary>
        /// 创建入库单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<OutboundRecDto> PostOutboundRecAsync(OutboundRecCreateOrUpdateDto input)
        {
            var OutboundRec = ObjectMapper.Map<OutboundRecCreateOrUpdateDto, OutboundRec>(input);
            OutboundRec.SetOrdNo(await _bizSeqService.GetAsync("出库单号"));
            OutboundRec = await _OutboundRecRepository.InsertAsync(OutboundRec);
            var itemDto = ObjectMapper.Map<OutboundRec, OutboundRecDto>(OutboundRec);

            return itemDto;
        }

        /// <summary>
        /// 修改入库单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<OutboundRecDto> PutOutboundRecAsync(Guid id, OutboundRecCreateOrUpdateDto input)
        {
            var OutboundRec = await _OutboundRecRepository.GetAsync(id);
            OutboundRec = ObjectMapper.Map(input, OutboundRec);
            OutboundRec = await _OutboundRecRepository.InsertAsync(OutboundRec);
            var itemDto = ObjectMapper.Map<OutboundRec, OutboundRecDto>(OutboundRec);

            return itemDto;
        }

        /// <summary>
        /// 审核入库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task ChkOutboundRecAsync(ChkOutboundRecDto input)
        {
            var matlInvs = await _invManager.ChkOutboundRec(input.Id);
            await _matlInvRepository.InsertManyAsync(matlInvs);
        }

        /// <summary>
        /// 作废入库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteOutboundRecAsync(Guid id)
        {
            var OutboundRec = await _OutboundRecRepository.GetAsync(id);

            if (OutboundRec.OutboundStas == EnumOutboundStas.未审核)
            {
                OutboundRec.OutboundStas = EnumOutboundStas.已作废;
            }
        }

        /// <summary>
        /// 获取入库单明细列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<OutboundDetlDto>> GetOutboundDetlListAsync(OutboundDetlInput input)
        {
            var iQuery = await _OutboundDetlRepository.WithDetailsAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<OutboundDetl>, List<OutboundDetlDto>>(iQuery.ToList());

                return new PagedResultDto<OutboundDetlDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OutboundDetl>, List<OutboundDetlDto>>(items.ToList());

                return new PagedResultDto<OutboundDetlDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 创建入库单明细信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<OutboundDetlDto> PostOutboundDetlAsync(OutboundDetlCreateOrUpdateDto input)
        {
            var OutboundDetl = ObjectMapper.Map<OutboundDetlCreateOrUpdateDto, OutboundDetl>(input);

            if (string.IsNullOrEmpty(input.Bchno))
            {
                OutboundDetl.Bchno = await _bizSeqService.GetAsync("入库批次号");
            }

            OutboundDetl = await _OutboundDetlRepository.InsertAsync(OutboundDetl);
            var itemDto = ObjectMapper.Map<OutboundDetl, OutboundDetlDto>(OutboundDetl);

            return itemDto;
        }
    }
}
